//
//  ViewController.m
//  SBPlayer
//
//  Created by sycf_ios on 2017/4/10.
//  Copyright © 2017年 shibiao. All rights reserved.
//

#import "ViewController.h"
#import "SBPlayer.h"
#import <Masonry.h>
#import "HCYoutubeParser.h"


@interface ViewController () <SBPlayerDelegate> {
    NSURL *_urlToLoad;
}

@property (nonatomic,strong) SBPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /**
     使用xib请用第二种方法
     [self.player assetWithURL:[NSURL URLWithString:@"http://download.3g.joy.cn/video/236/60236937/1451280942752_hd.mp4"]];
     [self.player setTitle:@"这是一个标题"];
     */
    
//    _urlToLoad = [NSURL URLWithString:@"http://download.3g.joy.cn/video/236/60236937/1451280942752_hd.mp4"];
//    [self setupPlayerWithUrl:_urlToLoad];
//    return;
    
    [self setupPlayerWithUrl:_urlToLoad];
    
    NSURL* url = [NSURL URLWithString:@"https://www.youtube.com/embed/FXCCYB3Mdlg"];
    [HCYoutubeParser thumbnailForYoutubeURL:url thumbnailSize:YouTubeThumbnailDefaultHighQuality completeBlock:^(UIImage *image, NSError *error) {
        
        if (!error) {
            [HCYoutubeParser h264videosWithYoutubeURL:url completeBlock:^(NSDictionary *videoDictionary, NSError *error) {
                
                NSDictionary *qualities = videoDictionary;
                
                NSString *URLString = nil;
                if ([qualities objectForKey:@"small"] != nil) {
                    URLString = [qualities objectForKey:@"small"];
                }
                else if ([qualities objectForKey:@"live"] != nil) {
                    URLString = [qualities objectForKey:@"live"];
                }
                else {
                    [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Couldn't find youtube video" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil] show];
                    return;
                }
                _urlToLoad = [NSURL URLWithString:URLString];
                
                // start video with url
                
                [self.player playWithUrl:_urlToLoad];
            }];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            [alert show];
        }
    }];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}

- (void)dealloc {
    NSLog(@"==daelloc==");
    
    [_player stop];
}

- (IBAction)playOrPause:(id)sender {
    
    [self.player stop];
//    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (IBAction)onBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupPlayerWithUrl:(NSURL*)videoUrl {
    // 纯代码请用此种方法
    // http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8 直播网址
    // 初始化播放器
    // [NSURL URLWithString:@"http://download.3g.joy.cn/video/236/60236937/1451280942752_hd.mp4"]
    self.player = [[SBPlayer alloc] initWithUrl:videoUrl];
    self.player.playerDelegate = self;
    // 设置标题
    [self.player setTitle:@"这是一个标题"];
    // 设置播放器背景颜色
    self.player.backgroundColor = [UIColor blackColor];
    // 设置播放器填充模式 默认SBLayerVideoGravityResizeAspectFill，可以不添加此语句
    self.player.mode = SBLayerVideoGravityResizeAspectFill;
    // 添加播放器到视图
    [self.view addSubview:self.player];
    // 约束，也可以使用Frame
    [self.player mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_top);
        make.height.mas_equalTo(@250);
    }];
}

- (void)playerReattached {
    [self.view addSubview:self.player];
    //删除UIView animate可以去除横竖屏切换过渡动画
    [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        [self.player mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.left.mas_equalTo(self.view);
            make.top.mas_equalTo(self.view.mas_top);
            make.height.mas_equalTo(@250);
        }];
        [self.player layoutIfNeeded];
    } completion:nil];
}

@end
